import { BrowserRouter as Router, Routes, Route, Navigate } from "react-router-dom";
import Topbar from "./components/topbar/Topbar";
import "./App.css";
import Home from "./pages/home/Home";
import UserList from "./pages/userList/UserList";
import User from "./pages/user/User";
import NewUser from "./pages/newUser/NewUser";
import ProductList from "./pages/productList/ProductList";
import Product from "./pages/product/Product";
import NewProduct from "./pages/newProduct/NewProduct";
import Analytics from "./pages/analytics/Analytics";
import Profile from "./pages/profile/Profile";
import BusinessAdmin from "./pages/businessAdmin/BusinessAdmin";
import BusinessRegister from "./pages/businessRegister/BusinessRegister";
import SubmitResult from "./pages/submitResult/SubmitResult";
import StoreDetail from "./pages/storeDetail/StoreDetail";
import Login from "./pages/login/Login";

function App() {
  const isAuthenticated = localStorage.getItem('isAuthenticated') === 'true';

  return (
    <Router>
      <Routes>
        <Route path="/login" element={<Login />} />
        <Route path="/" element={<Navigate to="/dashboard" replace />} />
        <Route
          path="/dashboard"
          element={
            isAuthenticated ? (
              <div className="container">
                <Topbar />
                <div className="others">
                  <Home />
                </div>
              </div>
            ) : (
              <Navigate to="/login" replace />
            )
          }
        />
        <Route
          path="/*"
          element={
            isAuthenticated ? (
              <div className="container">
                <Topbar />
                <div className="others">
                  <Routes>
                    <Route path="/profile" element={<Profile />} />
                    <Route path="/admin/users" element={<UserList />} />
                    <Route path="/admin/businesses" element={<BusinessAdmin />} />
                    <Route path="/store/:storeId" element={<StoreDetail />} />
                    <Route path="/business/register" element={<BusinessRegister />} />
                    <Route path="/submit-result" element={<SubmitResult />} />
                    <Route path="/user/:userId" element={<User />} />
                    <Route path="/user/new" element={<NewUser />} />
                    <Route path="/products" element={<ProductList />} />
                    <Route path="/product/:productId" element={<Product />} />
                    <Route path="/product/new" element={<NewProduct />} />
                    <Route path="/analytics" element={<Analytics />} />
                    <Route path="*" element={<Navigate to="/dashboard" replace />} />
                  </Routes>
                </div>
              </div>
            ) : (
              <Navigate to="/login" replace />
            )
          }
        />
      </Routes>
    </Router>
  );
}

export default App;
